how to Convert Generic List to DataTable in c#
how to Convert Generic List to DataTable in c#
661
01-Dec-2020
Anonymous User
01-Dec-2020Here we are going to convert Generic List to datatable in c#.
Creating a simple Employee class following below:
And a List with some data following below:
List<Employee> Employee = new List<Employee>(){new Employee() { Name = "Pawan", salary = 15000, EmpId = 100 },
new Employee() { Name = "rajeev", salary = 25000, EmpId = 101},
new Employee() { Name = "John", salary = 21000, EmpId = 102 }
};
Create a method that will convert any type of Generic List to a DataTable.
To call the “ToDataTable” method on button Click, use the following Code:
List<Employee> Employee = new List<Employee>(){
new Employee() { Name = "Pawan", salary = 15000, EmpId = 100 },
new Employee() { Name = "rajeev", salary = 25000, EmpId = 101},
new Employee() { Name = "John", salary = 21000, EmpId = 102 }
};
DataTable dt = ToDataTable(Employee);
Example :
I hope it will help to you.